package com.starinside.wp.controller.api;

import com.starinside.wp.domain.WxUser;
import com.starinside.wp.foundation.model.ErrorCode;
import com.starinside.wp.foundation.model.StatusResponse;
import com.starinside.wp.foundation.query.base.PagerQuery;
import com.starinside.wp.service.TeachAPICallService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

import static com.starinside.wp.foundation.constant.RequestConstant.TICKET;


@RestController
@RequestMapping("/wx/api/leaveRecord")
@Api(value = "/wx/api/leaveRecord", tags = "请假")
@Slf4j
public class APILeaveRecordController {

    @Autowired
    private TeachAPICallService teachAPICallService;

//    @Autowired
//    private AppUserService appUserService;


    @ApiOperation("请假")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "leaveDate", value = "请假日期 yyyy/MM/dd ", example = "2021/12/12"),
            @ApiImplicitParam(name = "leaveType", value = "请假类型 1-上午 2-下午 3-全天"),
            @ApiImplicitParam(name = "content", value = "内容")
    })
    @RequestMapping(value = "/add", method = RequestMethod.POST)
        public StatusResponse addLeaveRecord(@RequestHeader(TICKET) String ticket, HttpServletRequest req, HttpServletResponse resp,
                                             String leaveDate, int leaveType, String content){

        WxUser user = (WxUser) req.getSession().getAttribute("loginUser");

        if(StringUtils.isEmpty(user.getParentId()) || StringUtils.isEmpty(user.getDefaultBabyId()) || StringUtils.isEmpty(user.getParentMobile())){
            return StatusResponse.error(ErrorCode.CHILDREN_NOT_EXIST,"当前账号下没有宝宝信息");
        }

        Map record = teachAPICallService.addParentsRecord(user.getParentMobile(), user.getDefaultBabyId(), leaveDate, leaveType, content);

        return StatusResponse.success(record);
    }

    /**微信端
     * 查询家长的投诉列表
     * @param query
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation("查询请假列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "childrenId", value = "宝宝id，若没有则使用默认宝宝id"),
            @ApiImplicitParam(name = "month", value = "月份， 如2021/10"),
            @ApiImplicitParam(name = "pageNum", value = "页码"),
            @ApiImplicitParam(name = "pageSize", value = "页容量")
    })
    public StatusResponse getLeaveRecordList(@RequestHeader(TICKET) String ticket, HttpServletRequest req, HttpServletResponse resp, String childrenId, String month, PagerQuery query){
        if(org.apache.commons.lang.StringUtils.isEmpty(childrenId)) {
            WxUser user = (WxUser) req.getSession().getAttribute("loginUser");

            if (StringUtils.isEmpty(user.getParentId()) || StringUtils.isEmpty(user.getDefaultBabyId()) || StringUtils.isEmpty(user.getParentMobile())) {
                return StatusResponse.error(ErrorCode.CHILDREN_NOT_EXIST, "当前账号下没有宝宝信息");
            }

            childrenId = user.getDefaultBabyId();
        }
        Map record = teachAPICallService.getWxLeaveRecord(childrenId, month, query.getPageNum(), query.getPageSize());

        return StatusResponse.success(record);
    }

}
